package com.jinzhi.jzweb.controller;


import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationBatchComSDO;
import com.jinzhi.jzweb.service.ExaminationBatchComSService;
import com.jinzhi.sys.service.SysUserRelevancyService;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *
 * <pre>
 * 竞赛考试批次管理
 * </pre>
 * <small> 2023-05-07 14:59:39 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationBatchComS")
public class ExaminationBatchComSController extends BaseController {
    @Autowired
    private ExaminationBatchComSService examinationBatchComService;
    @Autowired
    SysUserRelevancyService userRelevancyService;
    @Autowired
    UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @GetMapping()
    @RequiresPermissions("jzweb:examinationBatchCom:examinationBatchCom")
    String ExaminationBatchCom(Integer type,Model model){
        model.addAttribute("type",type);
        return "jzweb/examinationBatchCom/examinationBatchCom";
    }



    @GetMapping("/add")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    String add(){
        return "jzweb/examinationBatchCom/adds";
    }


    @GetMapping("/adds/{id}")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    String adds(@PathVariable("id") Long id,Model model,Long cbatchId){
        model.addAttribute("id", id);
        model.addAttribute("cbatchId", cbatchId);
        return "jzweb/examinationBatchCom/adds";
    }



    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    String edit(@PathVariable("id") Long id,Model model){
        ExaminationBatchComSDO examinationBatchCom = examinationBatchComService.selectById(id);

        model.addAttribute("examinationBatchCom", examinationBatchCom);
        return "jzweb/examinationBatchCom/edit";
    }

    @Log("添加考试批次")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    public Result<String> save( ExaminationBatchComSDO examinationBatchCom){
        examinationBatchCom.setPId(examinationBatchCom.getIds());
        ExaminationBatchComSDO examinationBatchComSDO= examinationBatchComService.findOneByKv
                ("name",examinationBatchCom.getName().replaceAll(" ",""),
                        "pid",examinationBatchCom.getIds(),"cbatch_id",examinationBatchCom.getCbatchId());
        if(examinationBatchComSDO!=null){

            return Result.fail("赛场已存在！");
        }
        long m = ((examinationBatchCom.getEndTime().getTime()-examinationBatchCom.getStartTime().getTime())/1000/60);
        if(m<=0){
            return Result.fail("结束时间不能小于或等于开始时间！");

        }
        examinationBatchComService.insert(examinationBatchCom);
        return Result.ok();
    }

    @Log("查询对应赛道匹配的裁判")
    @ResponseBody
    @GetMapping("/getUserReferee")
    public Object getUserReferee( String track){
        if(StringUtils.isBlank(track)){
            return Result.fail("赛道不能为空！");
        }

//    List<SysUserRelevancyDO> list=    userRelevancyService.findByKv("track",track);
//        List<Map> list1=new ArrayList<>();
//        for(SysUserRelevancyDO li:list){
//            List<UserDO> listUser=    userService.findByKv("user_relevancy_id",li.getId());
//            Map map=new HashMap();
//            if(listUser.size()>0){
//                map.put("userId",listUser.get(0).getId());
//                map.put("name",listUser.get(0).getName());
//                list1.add(map);
//            }
//        }
        List<Map> list=  userRelevancyService.listUserRele(track);
        return Result.ok(list);
    }


    @Log("修改考试批次")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    public Result<String>  update( ExaminationBatchComSDO examinationBatchCom){

        boolean update = examinationBatchComService.updateById(examinationBatchCom);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考试批次")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationBatchCom:remove")
    public Result<String>  remove( Long id){
        examinationBatchComService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试批次")
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationBatchCom:batchRemove")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        examinationBatchComService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
